package com.xcdh.trading.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcdh.trading.entity.Tweet;
import com.xcdh.trading.mapper.TweetMapper;
import com.xcdh.trading.service.QQEmailService;
import com.xcdh.trading.service.TweetService;
import com.xcdh.trading.util.SSLUtils;
import com.xcdh.trading.util.TweetParser;
import lombok.SneakyThrows;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;

@Service
public class TweetServiceImpl extends ServiceImpl<TweetMapper, Tweet> implements TweetService {
    private final static String token = "AAAAAAAAAAAAAAAAAAAAALrgzQEAAAAAb1bt9ca7MZLJVo9gIe%2BrcCk1ghI%3DakL8odD54LsUq9QFWSUN3Yeya6cTTGVN7m2PAbEzaswgGDiLXf" ;
    private final static  String msk_user_id = "44196397"; // 马斯克的用户 ID
    @Autowired
    private RestTemplate restTemplate ;
    @Autowired
    private QQEmailService emailService ;




    @Override
    public void syncMskMskTweet(){
        String apiUrl = "https://api.twitter.com/2/users/" + msk_user_id + "/tweets?max_results=50&tweet.fields=created_at";

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token);
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求实体
        HttpEntity<String> entity = new HttpEntity<>(headers);

        try {
            // 发送 GET 请求
            ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.GET, entity, String.class);

            // 处理响应
            if (response.getStatusCode() == HttpStatus.OK) {
                String responseBody = response.getBody();
                List<Tweet> tweets = TweetParser.parseTweets(responseBody);
                for (Tweet tweet : tweets) {
                    String text = tweet.getText();
                    if(text.toLowerCase().contains("jpg")
                      || text.toLowerCase().contains("png")
                      || text.toLowerCase().contains("jpeg")
                      || text.toLowerCase().contains("$")
                      || text.toLowerCase().contains(".com")
                    ){
                        Tweet tweet1 = getById(tweet.getId());
                        if(tweet1 == null){
                            emailService.sendQQEmail("新信息注意查看" , text + " " + tweet1.getPostDate());
                        }else{
                            tweet.setCreateTime(null);
                        }
                    }
                    saveOrUpdate(tweet) ;
                }
            } else {
                System.out.println("请求失败，状态码：" + response.getStatusCode());
            }
        } catch (Exception e) {
            System.out.println("请求发生错误：" + e.getMessage());
        }
    }
}
